from PySide6.QtCore import QSize
from PySide6.QtGui import QAction, QIcon
from PySide6.QtWidgets import QApplication, QMainWindow, QToolBar, QStatusBar, QPushButton, QVBoxLayout, QWidget, \
    QLabel, QLineEdit, QStyle


# 主窗口类
class MainWindow(QMainWindow):
    def __init__(self, app) -> None:
        super().__init__()
        self.app = app  # 定义一个application对象
        self.setWindowTitle("Custom 主窗口")
        # self.addToolBarBreak()
        self.subWindow = SubWindow() # 子窗口

        # 1. 菜单栏 & 菜单
        menu_bar = self.menuBar()  # 菜单栏，没有时会自动创建
        file_menu = menu_bar.addMenu("&File")  # 创建名称为 File 的菜单, ！！！！！&的意思暂不明确
          # 在File菜单下，添加名为【Quit】的Action，并使用标准库的图标
        # quit_action = file_menu.addAction(self.style().standardIcon(QStyle.StandardPixmap.SP_DirOpenIcon), "Quit")  #
        quit_action = file_menu.addAction(self.style().standardIcon(QStyle.StandardPixmap.SP_DialogCloseButton), "Quit")
        quit_action.triggered.connect(self.quit_app)  # 给 quit_action 添加 triggered（触发） 事件绑定

        edit_menu = menu_bar.addMenu("Edit")
        edit_menu.addAction(self.style().standardIcon(QStyle.StandardPixmap.SP_DirLinkIcon), "Copy")
        edit_menu.addAction("Cut")
        edit_menu.addAction("Paste")
        edit_menu.addAction("Undo")
        edit_menu.addAction("Redo")

        # 加几个假菜单
        menu_bar.addMenu("Window")
        menu_bar.addMenu("Setting")
        menu_bar.addMenu("Help")

        # 2. Toolbars 工具栏
        toolbar = QToolBar("My Main toolbar")
        toolbar.setIconSize(QSize(16, 16))
        self.addToolBar(toolbar)

        # 给工具栏添加 Action（此处因为 quit_action 添加了图标，所以工具栏上现在只能看到图标了）
        toolbar.addAction(quit_action)

        action1 = QAction("Some Action", self)
        action1.setStatusTip("Status message for action1")
        action1.triggered.connect(self.toolbar_btn_click)
        toolbar.addAction(action1)

        toolbar.addSeparator()  # toolbar 的分隔线(在toolbar的两个action中间的竖线)

        # 用图标标识按钮
        action2 = QAction(QIcon("./01_BV16d4y1s73t_202403/resources/start.png"), "Some other Action", self)
        action2.setStatusTip("Status message for action2")
        action2.triggered.connect(self.toolbar_btn_click)
        action2.setCheckable(True)
        toolbar.addAction(action2)

        # 直接在toolbar上添加按键
        toolbar.addWidget(QPushButton('Click Here'))

        # 3. 状态栏 StatusBar
        self.setStatusBar(QStatusBar(self))

        # 4. 主窗口添加内容
        content_widget = QWidget()
        layout = QVBoxLayout()
        btn1 = QPushButton('打开/关闭子窗口')
        # btn1.clicked.connect(lambda: self.subWindow.show())
        btn1.clicked.connect(self.subWindow.showHide)

        layout.addWidget(btn1)
        # btn2 = QPushButton('btn2')
        # layout.addWidget(btn2)
        content_widget.setLayout(layout)
        self.setCentralWidget(content_widget)

    def quit_app(self):
        self.app.quit()

    def toolbar_btn_click(self, checked: bool):
        print("action triggered!", checked)
        self.statusBar().showMessage("Message from my app", 3000)  # 状态栏显示信息，显示时长为3s


class SubWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.lb = QLabel('这是子窗口')
        self.lineEdit = QLineEdit()
        self.lineEdit.setText('子窗口文本框')

        self.mainLayout = QVBoxLayout()
        self.mainLayout.addWidget(self.lb)
        self.mainLayout.addWidget(self.lineEdit)
        self.setLayout(self.mainLayout)

    def showHide(self):
        if self.window().isVisible():
            print("窗口当前是可见的（显示状态）")
            self.close()
            # self.hide()
        else:
            print("窗口当前是隐藏的")
            self.show()



application = QApplication()

custom_mainwindow = MainWindow(application)
custom_mainwindow.show()

application.exec()
